<template>
  <div class="home">
    <!-- 背景阴影 -->
    <div class="bg-mask" :style="getBackgroundImage" />

    <!-- 导航和轮播 -->
    <div class="home-container m-center">
      <home-nav />
      <mooc-carousel :height="316" trigger="click" @change="handelCarouselChange">
        <mooc-carousel-item v-for="(item,index) in swiperList" :key="index">
          <img :src="item.img" class="swiper-img">
        </mooc-carousel-item>
      </mooc-carousel>

      <home-banner />
    </div>



    <!-- 新上好课 -->
    <div class="course-list-container m-center">
      <!-- 标题 -->
      <h2 class="home-title">
        <i class="title-icon left-icon" :style="getBackgroundPosition(newList.leftIcon)" />
        {{ newList.title }}
        <i class="title-icon right-icon" :style="getBackgroundPosition(newList.rightIcon)" />
      </h2>

      <!-- 课程列表 -->
      <ul class="course-list">
        <li v-for="(item,index) in newList.data" :key="index" class="course-item" @click="courseClick(item.id)">
          <div class="img-box">
            <img :src="item.coverPath" alt="">
                    <div class="tags">
                      <span v-for="(tag, index) in item.type" :key="index" class="tag-item">{{ tag }}</span>
                    </div>
          </div>
          <p class="course-name">
            {{ item.className }}
          </p>
          <p class="info">
            <span>新课</span>
            <span>{{ item.level }}</span>
            <span><i class="iconfont icon-user">&#xe607;</i>{{ item.studyNumber }}</span>
            <span>
          <mooc-star class="star-box" :value="item.score" :disabled="true" />
        </span>
          </p>
        </li>
      </ul>
    </div>

    <!-- 新手入门 -->
    <div class="course-list-container m-center">
      <!-- 标题 -->
      <h2 class="home-title">
        <i class="title-icon left-icon" :style="getBackgroundPosition(guideList.leftIcon)" />
        {{ guideList.title }}
        <i class="title-icon right-icon" :style="getBackgroundPosition(guideList.rightIcon)" />
      </h2>

      <!-- 课程列表 -->
      <ul class="course-list">
        <li v-for="(item,index) in guideList.data" :key="index" class="course-item" @click="courseClick(item.id)">
          <div class="img-box">
            <img :src="item.coverPath" alt="">
            <div class="tags">
              <span v-for="(tag, index) in item.type" :key="index" class="tag-item">{{ tag }}</span>
            </div>
          </div>
          <p class="course-name">
            {{ item.className }}
          </p>
          <p class="info">
            <span>新手</span>
            <span>{{ item.level }}</span>
            <span><i class="iconfont icon-user">&#xe607;</i>{{ item.studyNumber }}</span>
            <span>
          <mooc-star class="star-box" :value="item.score" :disabled="true" />
        </span>
          </p>
        </li>
      </ul>
    </div>


    <div class="course-list-container m-center">
      <!-- 标题 -->
      <h2 class="home-title">
        <i class="title-icon left-icon" :style="getBackgroundPosition(improveList.leftIcon)" />
        {{ improveList.title }}
        <i class="title-icon right-icon" :style="getBackgroundPosition(improveList.rightIcon)" />
      </h2>

      <!-- 课程列表 -->
      <ul class="course-list">
        <li v-for="(item,index) in improveList.data" :key="index" class="course-item" @click="courseClick(item.id)">
          <div class="img-box">
            <img :src="item.coverPath" alt="">
            <div class="tags">
              <span v-for="(tag, index) in item.type" :key="index" class="tag-item">{{ tag }}</span>
            </div>
          </div>
          <p class="course-name">
            {{ item.className }}
          </p>
          <p class="info">
            <span>提升</span>
            <span>{{ item.level }}</span>
            <span><i class="iconfont icon-user">&#xe607;</i>{{ item.studyNumber }}</span>
            <span>
          <mooc-star class="star-box" :value="item.score" :disabled="true" />
        </span>
          </p>
        </li>
      </ul>
    </div>


    <div class="course-list-container m-center">
      <!-- 标题 -->
      <h2 class="home-title">
        <i class="title-icon left-icon" :style="getBackgroundPosition(technologyList.leftIcon)" />
        {{ technologyList.title }}
        <i class="title-icon right-icon" :style="getBackgroundPosition(technologyList.rightIcon)" />
      </h2>

      <!-- 课程列表 -->
      <ul class="course-list">
        <li v-for="(item,index) in technologyList.data" :key="index" class="course-item" @click="courseClick(item.id)">
          <div class="img-box">
            <img :src="item.coverPath" alt="">
            <div class="tags">
              <span v-for="(tag, index) in item.type" :key="index" class="tag-item">{{ tag }}</span>
            </div>
          </div>
          <p class="course-name">
            {{ item.className }}
          </p>
          <p class="info">
            <span>前沿</span>
            <span>{{ item.level }}</span>
            <span><i class="iconfont icon-user">&#xe607;</i>{{ item.studyNumber }}</span>
            <span>
          <mooc-star class="star-box" :value="item.score" :disabled="true" />
        </span>
          </p>
        </li>
      </ul>
    </div>

  </div>
</template>
<script>
import HomeNav from './home-nav.vue'
import HomeBanner from './home-banner.vue'
import { getSliderList } from 'api/home.js'
import { ERR_OK } from 'api/config.js'
import {getCourseFront, getCourseLevel, getNewCourse, viewSite} from "../../api/home";
export default {
  name: 'Home',
  data () {
    return {
      swiperList: [], //首页轮播信息
      technologyList: {}, // 前言技术
      improveList: {}, // 技能提升
      guideList: {}, // 新手入门
      newList: {}, // 新上好课
      practiceList: {}, // 实战课程
      currentSwiper: '' // 当前的轮播图片
    }
  },
  mounted () {
    this.getBanner()
    this.getCourseList()
    this.viewSite()
    this.getArticleList()
    this.getTeacherList()
    this.getAllStarList()
  },
  methods: {
    // 滚动轮播滚动完毕事件
    handelCarouselChange (index) {
      this.currentSwiper = this.swiperList[index].img
    },
    // 首页轮播信息
    getBanner () {
      getSliderList().then((res) => {
        let { code, data } = res
        if (code === ERR_OK) {
          this.swiperList = data
          if (this.swiperList.length > 0) {
            this.currentSwiper = this.swiperList[0].img
          }
        }
      })
    },
    getBackgroundPosition (position) {
      return {
        'background-position': position
      }
    },
    viewSite(){
      viewSite();
    },
    // 获取课程信息
    getCourseList () {

      getNewCourse().then(res => {
        this.newList.data = res.data

        this.newList.data.forEach(item=>{
          item.type = item.type.split(',')
        })
        console.log(this.newList)
        this.newList.title = '新／上／好／课'
        this.newList.leftIcon = 'center -72px'
        this.newList.rightIcon = 'center -108px'
      }),
              getCourseFront().then(res => {
                this.technologyList.data = res.data
                this.technologyList.data.forEach(item=>{
                  item.type = item.type.split(',')
                })
                this.technologyList.title = '前／沿／技／术'
                this.technologyList.leftIcon = 'center -288px'
                this.technologyList.rightIcon = 'center -324px'
              }),
              getCourseLevel('1,2').then(res => {

                this.guideList.data = res.data
                this.guideList.data.forEach(item=>{
                  item.type = item.type.split(',')
                })
                this.guideList.title = '新／手／入／门'
                this.guideList.leftIcon = 'center -144px'
                this.guideList.rightIcon = 'center -180px'
              }),
              getCourseLevel('3,4').then(res => {

                this.improveList.data = res.data
                this.improveList.data.forEach(item=>{
                  item.type = item.type.split(',')
                })
                this.improveList.title = '技／能／提／升'
                this.improveList.leftIcon = 'center -216px'
                this.improveList.rightIcon = 'center -252px'
              })



    },
    courseClick(id){
      this.$router.push({ path: `/course/${id}` })
    },
  },
  computed: {
    getBackgroundImage () {
      return {
        'background-image': `url(${this.currentSwiper})`
      }
    }
  },
  components: {
    HomeNav,
    HomeBanner,
  }
}
</script>
<style lang="stylus" scoped>
  @import '~assets/stylus/variables.styl';
  @import '~assets/stylus/mixin.styl';
  .home
    margin-top: 32px;
    min-height: 750px;
    .bg-mask
      position: absolute;
      top: 0;
      background-size: cover;
      transition: all 0.3s;
      width: 100%;
      height: 180px;
      opacity: 0.3;
      filter: blur(100px);
    .home-container
      position: relative;
      box-shadow: 0 12px 24px 0 $shadow;
      border-radius: 8px;
      .mooc-carousel
        position: absolute;
        left: 216px;
        top: 0;
        right: 0;
        >>> .mooc-carousel-indicators
          left: initial;
          right: 20px;
          transform: none;
        .swiper-img
          display: block;
          width: 100%;
          height: 316px;
          border-top-right-radius: 8px;
  .course-list-container
    padding: 36px 0 48px;
    .split-banner
      display: flex;
      align-items: center;
      margin-top: 24px;
      .split-banner-item
        position: relative;
        flex: 1;
        margin: 0 8px 16px 8px;
        height: 108px;
        background-color: rgba(7,17,27,0.5);
        & > img
          display: block;
          width: 100%;
          height: 100%;
          background-color: #ccc;
          border-radius: 8px;
        .title
          z-index: 3;
          position: absolute;
          left: 0;
          right: 0;
          color: #fff;
          text-align: center;
          &.main
            top: 30%;
            font-size: 20px;
            line-height: 28px;
            font-weight: 700;
          &.sub
            top: 55%;
            font-size: 12px;
            line-height: 24px;
        .mask
          z-index: 1;
          position: absolute;
          left: 0;
          right: 0;
          top: 0;
          bottom: 0;
          background-color: rgba(7,17,27,0.4);
          border-radius: 8px;
    .course-list
      margin-top: 24px;
  .course-list
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
    .course-item
      position: relative;
      flex: 0 0 20%;
      margin-bottom: 32px;
      cursor: pointer;
      &:hover
        .course-name
          color: $theme-red-color;
      .img-box
        position: relative;
        margin: 0 auto 8px;
        img-box(216px, 120px)
        & > img
          border-radius: 8px;
          &:hover
            box-shadow: 0px 12px 24px  $shadow
        .tags
          position: absolute;
          bottom: 12px;
          left: 8px;
          .tag-item
            margin-right: 10px;
            padding: 4px 8px;
            border-radius: 4px;
            color: #fff;
            font-size: 12px;
            background-color: rgba(28, 31, 33, 0.6);
        .badge
          position: absolute;
          width: 42px;
          border: 2px solid #fff;
          border-radius: 12px;
          line-height: 20px;
          color: #fff;
          font-size: 12px;
          text-align: center;
          font-weight: 600;
        .rate
          right: -7px;
          top: 8px;
          background-image: linear-gradient(-90deg, #65da98 0%, #0cba4d 100%);
        .script
          left: -7px;
          top: 8px;
          background-image: linear-gradient(90deg, #ffaa00 17%, #f76b1c 100%)
      & > p
        padding: 0 8px;
        box-sizing: border-box;
      .course-name
        height: 46px;
        word-break: break-all;
        font-size: 16px;
        font-weight: 700;
        line-height: 24px;
        color: $font-first-color;
        multline-ellipsis(2);
      .info
        margin-top: 5px;
        font-size: 12px;
        color: $font-four-color;
        line-height: 24px;
        & > span
          padding: 0 6px;
          &:first-child
            padding-left: 0;
        .icon-user
          margin-right: -2px;
          font-weight: 600;

      .price
        span
          color: $font-second-color;
          font-size: 12px;
          font-weight: 800;
          line-height: 24px;
          letter-spacing: 0.5px;
        .old-price
          padding:0 3px 0 10px;
          text-decoration: line-through;
        .discount-title
          padding: 0 4px;
          border-radius: 4px;
          font-size: 12px;
          background-color: $theme-red-color;
          opacity: 0.6;
          color: #fff;
</style>
